package com.xff.Controller;

import com.xff.bean.AdminBean;
import com.xff.bean.BookBean;
import com.xff.dao.AdminDao;
import com.xff.dao.BookDao;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@Controller
public class UserController {

    @PostMapping("/AddUserServlet")
    public String AddUserServlet(HttpServletRequest request, HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            //获取要添加的读者信息
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String name = request.getParameter("name");
            String email = request.getParameter("email");
            String phone = request.getParameter("phone");
            String sex = request.getParameter("sex");
            String hobby = request.getParameter("hobby");
            System.out.println(username);
            int lend_num = Integer.parseInt(request.getParameter("lend_num"));
            int max_num = Integer.parseInt(request.getParameter("max_num"));
            //调用函数添加读者信息
            AdminDao userdao = new AdminDao();
            userdao.Register(username, password, name, email, phone, lend_num, max_num,sex,hobby,0);
            return "redirect:/admin_user";
        }
        else {
            return "redirect:/login";
        }
    }

    @RequestMapping("/DeleteUserServlet")
    public String DeleteUserServlet(HttpServletRequest request,HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            //删除读者信息
            //Integer 类：parseInt() 方法用于将字符串参数作为有符号的十进制整数进行解析
            int aid = Integer.parseInt(request.getParameter("aid"));
            AdminDao admindao = new AdminDao();
            admindao.deleteUser(aid);
            return "redirect:/admin_user";
        }
        else {
            return "redirect:/login";
        }
    }

    @PostMapping("/UpdateUserServlet")
    public  String UpdateUserServlet(HttpServletRequest request,HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            //修改读者信息
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String name = request.getParameter("name");
            String email = request.getParameter("email");
            String phone = request.getParameter("phone");
            String sex = request.getParameter("sex");
            String hobby = request.getParameter("hobby");
            //Integer 类：parseInt() 方法用于将字符串参数作为有符号的十进制整数进行解析
            //借阅最大天数
            int max_num = Integer.parseInt(request.getParameter("max_num"));
            //最大可借数
            int lend_num = Integer.parseInt(request.getParameter("lend_num"));
            //用户id
            int aid = Integer.parseInt(request.getParameter("aid"));
            System.out.println(username);

            AdminDao userdao = new AdminDao();
            userdao.updateUser(aid, username, password, name, email, phone, lend_num, max_num,sex,hobby);
            return "redirect:/admin_user";
        }
        else {
            return "redirect:/login";
        }
    }

    @PostMapping("/AdminServlet")
    public  String AdminServlet(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {
        if (session.getAttribute("aid")!=null) {
            //PrintWriter:输出流，为异常处理
            PrintWriter out = response.getWriter();
            BookDao bookdao = new BookDao();
            //这里为了简单，设置了tip，用来区分是修改密码功能，还是修改个人资料的功能，tip=1为修改密码
            int tip = Integer.parseInt(request.getParameter("tip"));
            //获取发起请求页面的文件名称，这个在对应的jsp里面的表单填写，修改完成后就可以直接返回对应的页面
            String url = request.getParameter("url");
            AdminBean adminbean = new AdminBean();
            //获取存到session的aid
            String aid = (String) session.getAttribute("aid");
            AdminDao admindao = new AdminDao();
            //通过aid获取到读者的信息
            adminbean = admindao.get_AidInfo2(aid);
            //修改密码
            if (tip == 1) {
                //获取到输入的旧密码，新密码
                String password = request.getParameter("password");
                String password2 = request.getParameter("password2");

                //获取读者数据表中的密码
                String old_password = adminbean.getPassword();
                //对旧密码进行比较，如果相同就修改，不相同就直接退出
                if (old_password.equals(password)) {
                    admindao.updateUser(adminbean.getAid(), adminbean.getUsername(), password2, adminbean.getName(),
                            adminbean.getEmail(), adminbean.getPhone(), adminbean.getLend_num(), adminbean.getMax_num(),adminbean.getSex(),adminbean.getHobby());
                    return "redirect:/" + url;
                } else {
                    out.write("<script type='text/javascript'>alert('password error');location.href='/" + url + "';  </script>");

                }
            } else {
                //修改个人资料
                //获取输入的信息
                String name = request.getParameter("name");
                String email = request.getParameter("email");
                String phone = request.getParameter("phone");
                //修改输入的信息到数据表中
                admindao.updateUser(adminbean.getAid(), adminbean.getUsername(), adminbean.getPassword(), name,
                        email, phone, adminbean.getLend_num(), adminbean.getMax_num(),adminbean.getSex(),adminbean.getHobby());
                return "redirect:/" + url;
            }
            return "redirect:/" + url;
        } else
            return "redirect:/login";

    }

    @PostMapping("/SelectUserServlet")
    public  String SelectServlet(HttpServletRequest request,HttpSession session){
        if(session.getAttribute("status")!=null&&(int)session.getAttribute("status")==2) {
            String name = request.getParameter("name");
            AdminDao adminDao = new AdminDao();
            ArrayList<AdminBean> data = adminDao.getLikeList(name);
            //将获取的结果存入请求中
            request.setAttribute("data", data);
            return "admin_user";
        }else
            return "redirect:/login";
    }

    @ResponseBody
    @GetMapping("/updateInfor")
    public String updateInfor(HttpServletRequest request){
        String attribute=request.getParameter("attribute");
        String username=request.getParameter("username");
        String value=request.getParameter("value");
        AdminDao adminDao=new AdminDao();
        adminDao.updateonevalue(attribute,value,username);
        return "ok";
    }
}
